<?php
include_once "class/WXBizMsgCrypt.php";

$encodingAesKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C";
$token = "weixin";
$corpId = "wx5823bf96d3bd56c7";

$sReqMsgSig = $_REQUEST['msg_signature'];
$sReqTimeStamp = $_REQUEST['timestamp'];
$sReqNonce = $_REQUEST['nonce'];

$wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);

//post请求的密文数据
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if ($_REQUEST['encrypt_type'] == 'aes'){
    $decryptMsg = "";  //解密后的明文
    @$errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $postStr, $decryptMsg);
	if ($errCode == 0) {
		$sMsg = $decryptMsg;
		echo "\n---------------------------明文---------------------------\n".$sMsg;
		// 解密成功，sMsg即为xml格式的明文
		// TODO: 对明文的处理
		// For example:
		//$xml = new DOMDocument();
		//$xml->loadXML($sMsg);
		//$content = $xml->getElementsByTagName('Content')->item(0)->nodeValue;
		//print("content: " . $content . "\n\n");
	} else {
		print("ERR: " . $errCode . "\n\n");
		//exit(-1);
	}
}